package liang.jiaxin.com.controller;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import liang.jiaxin.com.domain.Bbs;
import liang.jiaxin.com.dto.ResponseEntity;
import liang.jiaxin.com.dto.Result;
import liang.jiaxin.com.dto.request.bbs.BbsRequestDTO;
import liang.jiaxin.com.dto.response.bbs.BbsResponseDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.http.MediaType;
import liang.jiaxin.com.service.BbsService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 *   前端控制器
 * </p>
 *
 * @author Jrxsr
 * @since 2019-04-13
 */
@RestController
@RequestMapping("/api/bbs")
@Api(value = "接口", description = "接口")
public class BbsController{

	private static Logger logger = LoggerFactory.getLogger(BbsController.class);

	@Resource
	private BbsService bbsService;

	@GetMapping(value = "/{id}")
	@ApiOperation(value = "详情", produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity get(@PathVariable("id") Long id) {
		Bbs bbs = bbsService.getById(id);
		BbsResponseDTO responseDTO = new BbsResponseDTO();
		ResponseEntity responseEntity = new ResponseEntity();
		if (bbs != null) {
			BeanUtils.copyProperties(bbs, responseDTO);
			responseEntity.setData(responseDTO);
			responseEntity.setMessage("获取成功！");
			return Result.warsucceRes(responseEntity);
		} else {
			responseEntity.setMessage("获取失败！");
			return Result.warerrorRes(responseEntity);
		}
	}

    @PostMapping(value = "/queryList")
    @ApiOperation(value = "查看", produces = MediaType.APPLICATION_JSON_VALUE)
    public List<BbsResponseDTO> queryList() {
        return bbsService.queryList();
    }


	@PostMapping(value = "/notice")
	@ApiOperation(value = "通用回调", produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity notice(@RequestBody Map map) {
		ResponseEntity responseEntity = new ResponseEntity();
		responseEntity.setData(JSON.toJSONString(map));
		responseEntity.setMessage("回调成功！");
		System.out.println("通用回调:" + JSON.toJSONString(map));
		return Result.warsucceRes(responseEntity);
	}

	@PostMapping(value = "/insert")
	@ApiOperation(value = "新增", produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity insert(@RequestBody BbsRequestDTO requestDTO) {
		logger.info(JSON.toJSONString(requestDTO));
		requestDTO.setCreateDatetime(new Date());
//		requestDTO.setCreateOperator("*神秘人*");
		requestDTO.setUpdateDatetime(new Date());
		requestDTO.setUpdateOperator(requestDTO.getCreateOperator());
		ResponseEntity responseEntity = new ResponseEntity();
		responseEntity.setData(bbsService.insert(requestDTO));
		responseEntity.setMessage("新增成功！");
		return Result.warsucceRes(responseEntity);
	}
}